package com.xoa.dev.weLink.controller;

import com.xoa.dev.weLink.service.WeLinkConfigService;
import com.xoa.dev.weLink.service.WeLinkService;
import com.xoa.model.weLink.UserWeLink;
import com.xoa.util.CookiesUtil;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("weLink")
public class WeLinkController {

    // 设置主页
    @RequestMapping("/index")
    public String index(){
        return "app/weLink/index";
    }

    // 基础参数设置页面
    @RequestMapping("/parameterSetting")
    public String setting(){
        return "app/weLink/parameterSetting";
    }

    // 绑定OA用户页面
    @RequestMapping("/oaBinding")
    public String oaBinding(){
        return "app/weLink/oaBinding";
    }

    // 同步组织用户界面
    @RequestMapping("/weLinkToOa")
    public String weLinkToOa(){
        return "app/weLink/weLinkToOa";
    }

    @Resource
    private WeLinkService weLinkService;

    @Resource
    private WeLinkConfigService weLinkConfigService;


    /**
     * @接口说明: welink测试连接
     * @日期: 2020/3/13
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("weLinkContent")
    public BaseWrapper content(){
        BaseWrapper baseWrapper = new BaseWrapper();
        String accessToken = weLinkService.getAccessToken();
        if(!StringUtils.checkNull(accessToken)){
            baseWrapper.setFlag(true);
        }else{
            baseWrapper.setFlag(false);
        }
        return baseWrapper;
    }

    /**
     * @接口说明: 获取welink配置
     * @日期: 2020/3/13
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("getWeLinkConfig")
    public BaseWrapper getWeLinkConfig(){
        return weLinkConfigService.getWeLinkConfig();
    }

    /**
     * @接口说明: 设置welink配置
     * @日期: 2020/3/13
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("setWeLinkConfig")
    public BaseWrapper setWeLinkConfig(String weLinkAppId,String weLinkAppSecret){
        return weLinkConfigService.setWeLinkConfig(weLinkAppId,weLinkAppSecret);
    }

    /**
     * @接口说明: 获取绑定的user
     * @日期: 2020/4/22
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("selUsersWithBindType")
    public BaseWrapper selUsersWithBindType(String deptId,String page,String pageSize){
        return weLinkConfigService.selUsersWithBindType(deptId, page, pageSize);
    }

    /**
     * @接口说明: 绑定Oa用户
     * @日期: 2020/3/13
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("bindOaUser")
    public BaseWrapper bindOaUser(UserWeLink userWeLink){
        return weLinkConfigService.bindOaUser(userWeLink);
    }

    /**
     * @接口说明: 解绑oa用户
     * @日期: 2020/3/13
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("removeBindOaUser")
    public BaseWrapper removeBindOaUser(UserWeLink userWeLink){
        return weLinkConfigService.removeBindOaUser(userWeLink);
    }

    /**
     * @接口说明: 根据code获取用户信息
     * @日期: 2020/3/13
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("getWlUserIdByCode")
    public String getWlUserIdByCode(HttpServletResponse response,String code){
        CookiesUtil.setCookie(response, "thirdAssessToken", code);
        return weLinkService.getWlUserIdByCode(code);
    }


    /**
     * @接口说明: 获取单个部门信息
     * @日期: 2020/3/13
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("getDepartment")
    public BaseWrapper getDepartment(String deptId){
        BaseWrapper baseWrapper = new BaseWrapper();
        baseWrapper.setData(weLinkService.getDepartment(deptId));
        baseWrapper.setFlag(true);
        return baseWrapper;
    }

    /**
     * @接口说明: 获取子部门信息
     * @日期: 2020/3/13
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("getDepartments")
    public BaseWrapper getDepartments(String deptId){
        BaseWrapper baseWrapper = new BaseWrapper();
        baseWrapper.setData(weLinkService.getDepartments(deptId));
        baseWrapper.setFlag(true);
        baseWrapper.setCode("0");
        return baseWrapper;
    }

    /**
     * @接口说明: 获取部门下的用户信息
     * @日期: 2020/3/13
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("getUsersByDeptId")
    public BaseWrapper getUsersByDeptId(String deptCode, String page, String pageSize){
        BaseWrapper baseWrapper = new BaseWrapper();
        baseWrapper.setData(weLinkService.getUsersByDeptId(deptCode, page, pageSize));
        baseWrapper.setFlag(true);
        baseWrapper.setCode("0");
        return baseWrapper;
    }


    /**
     * @接口说明: 同步部门接口
     * @日期: 2020/4/22
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("WlDeptToOA")
    public String WlDeptToOA(){
        return weLinkService.WlDeptToOA();
    }

    /**
     * @接口说明: 同步用户接口
     * @日期: 2020/4/22
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("WlUserToOA")
    public String WlUserToOA(HttpServletRequest request){
        return weLinkService.WlUserToOA(request);
    }


    /**
     * @接口说明: 同步部门和用户接口
     * @日期: 2020/4/22
     * @作者: 张航宁
     */
    @ResponseBody
    @RequestMapping("WlDeptUserToOA")
    public String WlDeptUserToOA(HttpServletRequest request){
        weLinkService.WlDeptToOA();
        weLinkService.WlUserToOA(request);
        return "true";
    }

}
